package com.example.xuanke.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xuanke.Entity.StudentEntity;
import com.example.xuanke.commton.WrapMapper;
import com.example.xuanke.commton.Wrapper;
import com.example.xuanke.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentSerivce {
    @Autowired
    private StudentMapper studentMapper;
    //添加用户
  //添加用户
    public Wrapper<?> addUser(StudentEntity student) {
        // 严格参数校验
        if (student.getStudentNo() == null || student.getStudentNo().trim().isEmpty()) {
            return new Wrapper<>(500, "学号不能为空", false);
        }
        if (student.getUsername() == null || student.getUsername().trim().isEmpty()) {
            return new Wrapper<>(500, "用户名不能为空", false);
        }
        if (student.getPassword() == null || student.getPassword().trim().isEmpty()) {
            return new Wrapper<>(500, "密码不能为空", false);
        }
        
        try {
            // 检查学号是否已存在
            QueryWrapper<StudentEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("student_no", student.getStudentNo());
            if (studentMapper.selectCount(queryWrapper) > 0) {
                return new Wrapper<>(500, "该学号已被注册", false);
            }
            
            // 显式设置ID值为null，让MyBatis-Plus处理
            student.setId(null);
            

            
            // 插入数据
            int result = studentMapper.insert(student);
            
            if (result > 0) {
                return WrapMapper.ok(true);
            } else {
                return new Wrapper<>(500, "注册失败：数据库操作错误", false);
            }
        } catch (Exception e) {
            return new Wrapper<>(500, "注册失败：" + e.getMessage(), false);
        }
    }
            public Wrapper<Boolean> login(String studentNo, String password){
                if (studentNo != null && !studentNo.isEmpty() && password != null && !password.isEmpty()){
                    try {
                        // 创建查询条件
                        QueryWrapper<StudentEntity> queryWrapper = new QueryWrapper<StudentEntity>();
                        // 确保使用正确的列名，与数据库表一致
                        queryWrapper.eq("student_no", studentNo)
                                   .eq("password", password);
                        
                        // 查询数据库，表名由StudentEntity上的@TableName决定
                        StudentEntity student = studentMapper.selectOne(queryWrapper);
                        
                        if (student != null) {
                            // 找到匹配的学生记录，登录成功
                            return WrapMapper.ok(true);
                        } else {
                            // 没有找到匹配的学生记录，登录失败
                            return new Wrapper<>(500, "用户名或密码错误", false);
                        }
                    } catch (Exception e) {
                        return new Wrapper<>(500, "登录异常: " + e.getMessage(), false);
                    }
                } else {
                    return new Wrapper<>(500, "账号密码不能为空", false);
                }
            }
}
